package sjd.MyDataBase;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.io.*;
import sjd.util.*;
public class MyDataBase {
	private Connection connection;
	private Statement statement;
	public MyDataBase()throws Exception
	{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		connection=DriverManager.getConnection("jdbc:odbc:demo");
		statement=connection.createStatement();
	}
	public boolean existAccount(String number) throws SQLException
	{
		ResultSet tmpset;
		tmpset=statement.executeQuery("select * from AccountTable where number='"+number+"';");
		if(tmpset.next())
			return true;
		return false;
	}
	public boolean existAccount(String number,String passwd) throws SQLException
	{
		ResultSet tmpset;
		tmpset=statement.executeQuery("select * from AccountTable where number='"+number+"'AND passwd='"+passwd+"';");
		if(tmpset.next())
			return true;
		return false;
	}
	public boolean isFriend(String a,String b) throws SQLException
	{
		ResultSet tmpset;
		tmpset=statement.executeQuery("select * from FriendTable where number='"+a +"'AND friendnumber='"+b+"'");
		if(tmpset.next())
			return true;
		return false;
	}
	public boolean delFriend(String a,String b) throws SQLException
	{
		if(!isFriend(a,b))
			return false;
		statement.executeUpdate("delete from FriendTable where number='"+a+"' and friendnumber='"+b+"'");
		statement.executeUpdate("delete from FriendTable where number='"+b+"' and friendnumber='"+a+"'");
		return true;
	}
	
	public boolean addAcount(String number,String name,String passwd)throws SQLException
	{
		if(existAccount(number))
			return false;
		//statement.executeUpdate("insert into AccountTable values ( '"+456+"','"+456+"','"+789+"',"+0));
		statement.executeUpdate("insert into AccountTable values ('"+number+"','"+name+"','"+passwd+"','"+0+"')");
		return true;
	}
	public Object[] getFriend(String number) throws SQLException
	{
		ResultSet tmpset;
		//String[] tmp;
		HashSet a=new HashSet();
		tmpset=statement.executeQuery("select friendnumber from FriendTable where number='"+number+"';");
		while(tmpset.next())
		{
			a.add(tmpset.getString("friendnumber"));
		}
		return (a.toArray());
	}
	public Account getAccount(String number) throws SQLException
	{
		ResultSet tmpset;
		Account account = null;
		tmpset=statement.executeQuery("select * from AccountTable where number='"+number+"';");
		tmpset.next();
		account.setNumber(tmpset.getString("number"));
		account.setName(tmpset.getString("name"));
		account.setPasswd(tmpset.getString("passwd"));
		account.setIcon(tmpset.getInt("number"));
		return account;
	}
	
}